package game.targets.screen;

import game.targets.Constants.SOUND;
import game.targets.Locator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ScoreScreen implements Screen{
	private BitmapFont white, white96;
	private Stage stage;
	private Table table;
	private TextButton buttonExit, buttonPlay, buttonLevel;
	private Label heading, arrowsFired, targetsHit, score, badTargetsHit, bonusTargetsHit;
	private Skin skin;
	private TextureAtlas atlas;
	private TextButtonStyle textButtonStyle;
	private Texture background;
	private SpriteBatch uiBatch;
	private ScreenHandler screenHandler;
	
	public ScoreScreen(ScreenHandler h){
		this.screenHandler=h;
		stage = new Stage();
		uiBatch=new SpriteBatch();
		//background = new Texture(BACKGROUNDMAIN);
        //background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
		white = new BitmapFont(Gdx.files.internal("data/white40.fnt"), false);
		white96 = new BitmapFont(Gdx.files.internal("data/white40.fnt"), false);
		atlas = new TextureAtlas(Gdx.files.internal("data/button.pack"));
		skin = new Skin(atlas);
		table = new Table(skin);
		table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

		textButtonStyle = new TextButtonStyle();
		textButtonStyle.up = skin.getDrawable("button.up");
		textButtonStyle.down = skin.getDrawable("button.down");
		textButtonStyle.pressedOffsetX = 1;
		textButtonStyle.pressedOffsetY = -1;
		textButtonStyle.font = white;

		buttonPlay = new TextButton("Play Again", textButtonStyle);
		buttonPlay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				screenHandler.getRoomScreen().createNewRoom();
				Locator.getStats().clear();
				screenHandler.scoreToRoom();
			}
		});
		buttonPlay.pad(10.0f);

		buttonLevel = new TextButton("Menu", textButtonStyle);
		buttonLevel.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				Locator.getStats().clear();
				screenHandler.scoreToMenu();
			}
		});
		buttonLevel.pad(10.0f);

		// create buttons
		buttonExit = new TextButton("Exit", textButtonStyle);
		buttonExit.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				Gdx.app.exit();
			}
		});
		buttonExit.pad(10.0f);

		// create title
		LabelStyle headingStyle = new LabelStyle(white96, Color.WHITE);
		heading = new Label("How you did", headingStyle);
		arrowsFired= new Label("Arrows Fired  "+Locator.getStats().getArrowsFired(), headingStyle);
		targetsHit= new Label("Targets Hit  "+Locator.getStats().getTargetsHit(), headingStyle);
		score= new Label("Score  "+Locator.getStats().getScore(), headingStyle);
		badTargetsHit= new Label("Bad Targets Hit  "+Locator.getStats().getBadHit(), headingStyle); 
		bonusTargetsHit= new Label("Bonus Targets Hit  "+Locator.getStats().getBonusHit(), headingStyle);
		heading.setFontScale(1.0f);
		table.add(heading).left().row();
		table.add(score).left().row();
		table.add(arrowsFired).left().row();
		table.add(targetsHit).left().row();
		table.add(badTargetsHit).left().row();
		table.add(bonusTargetsHit).left().row();
		table.add(buttonPlay).right().row();
		table.add(buttonLevel).right().row();
		table.add(buttonExit).right();

		stage.addActor(table);

	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		/*uiBatch.begin();
        uiBatch.draw(background, 0, 0, background.getWidth() * COUNT, background.getHeight() * COUNT, 0, COUNT, COUNT, 0);
        uiBatch.end();*/
		stage.act(delta);
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, true);
		table.invalidateHierarchy();
		table.setSize(width, height);
	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
	}
	
	public void updateStats(){
		score.setText("Score  "+Locator.getStats().getScore());
		arrowsFired.setText("Arrows Fired  "+Locator.getStats().getArrowsFired());
		badTargetsHit.setText("Bad Targets Hit  "+Locator.getStats().getBadHit());
		bonusTargetsHit.setText("Bonus Targets Hit  "+Locator.getStats().getBonusHit());
		targetsHit.setText("Targets Hit  "+Locator.getStats().getTargetsHit());
	}

	@Override
	public void hide() {
		Gdx.input.setInputProcessor(null);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		stage.dispose();
		atlas.dispose();
		skin.dispose();
		white.dispose();
	}

}
